package org.apache.ocean;

import java.net.URL;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.atomic.AtomicLong;

import org.apache.lucene.index.Term;
import org.jdom.Element;

/**
 * 
 * @author jasonr
 */
public class NodeClient {
  
	public static OceanXML add(URL url, OceanDocument document, HttpTimeout httpTimeout) throws Exception {
		OceanXML request = new OceanXML();
    Element addElement = new Element("add");
    addElement.addContent(document.toElement());
    request.addBody(addElement);
    OceanXML response = OceanXML.request(url, request, httpTimeout);
    return response;
    //Element responseOceanDocumentElement = XMLUtil.getFirstChild(response.bodyElement);
    //if (responseOceanDocumentElement == null) throw new Exception("no ocean document returned");
    //return new OceanDocument(responseOceanDocumentElement);
	}
	
  public static OceanXML update(URL url, OceanDocument document, HttpTimeout httpTimeout) throws Exception {
    document.remove(ClusterConstants.SCORE_FIELD);
  	OceanXML updateXML = new OceanXML();
    Element updateElement = new Element("update");
    updateElement.addContent(document.toElement());
    updateXML.addBody(updateElement);
    OceanXML response = OceanXML.request(url, updateXML, httpTimeout);
    return response;
    //Element responseOceanDocumentElement = XMLUtil.getFirstChild(response.bodyElement);
    //if (responseOceanDocumentElement == null) throw new Exception("no ocean document returned");
    //return new OceanDocument(responseOceanDocumentElement);
  }
  
  public static SearchResponse search(URL url, SearchRequest searchRequest, HttpTimeout httpTimeout) throws Exception {
  	HttpParameters parameters = searchRequest.toHttpParameters();
  	OceanXML oceanXML = OceanXML.request(url, parameters, httpTimeout);
  	return new SearchResponse(oceanXML);
  }
  
  public static TermFrequencies getTermFrequencies(URL url, String queryString, HttpTimeout httpTimeout) throws Exception {
  	HttpParameters parameters = new HttpParameters("action", "gettermfrequencies", "q", queryString);
  	OceanXML response = OceanXML.request(url, parameters, httpTimeout);
  	List<Element> elements = XMLUtil.getChildren(response.bodyElement);
  	TermFrequencies termFrequencies = new TermFrequencies(elements);
  	return termFrequencies;
  }
  
  public static OceanXML getShardInfosOceanXML(URL url, HttpTimeout httpTimeout) throws Exception {
  	HttpParameters parameters = new HttpParameters("action", "getshardinfos");
  	return OceanXML.request(url, parameters, httpTimeout);
  }
  
  public static Map<String,ShardInfo> getShardInfos(URL url, HttpTimeout httpTimeout) throws Exception {
  	HttpParameters parameters = new HttpParameters("action", "getshardinfos");
  	OceanXML response = OceanXML.request(url, parameters, httpTimeout);
  	Map<String,ShardInfo> map = new HashMap<String,ShardInfo>();
  	for (Element element : XMLUtil.getChildren(response.bodyElement)) {
  		ShardInfo shardInfo = new ShardInfo(element);
  		map.put(shardInfo.id, shardInfo);
  	}
  	return map;
  }
  
  public static void addTransactionRecord(URL url, TransactionRecord transactionRecord, HttpTimeout httpTimeout) throws Exception {
  	OceanXML oceanRequest = new OceanXML("action", "addtransactionrecord");
  	oceanRequest.addBody(transactionRecord); 
  	OceanXML.request(url, oceanRequest, httpTimeout);
  }
  
  public static List<TransactionRecord> getTransactionRecords(URL url, String shardID, TransactionID startID, CLong length, HttpTimeout httpTimeout) throws Exception {
  	HttpParameters parameters = new HttpParameters("action", "gettransactionrecords", "shardid", shardID, "startid", startID, "length", length);
  	return OceanXML.request(url, parameters, httpTimeout).bodyToList(new TransactionRecord.TransactionRecordTransform());
  }
  
  //public static List<TransactionRecord> getTransactionRecords(URL url, int rows, List<TransactionRecordRequest> transactionRecordRequests, HttpTimeout httpTimeout) throws Exception {
   // HttpParameters parameters = new HttpParameters("action", "gettransactionrecords", "rows", rows);
   // for (TransactionRecordRequest transactionRecordRequest : transactionRecordRequests) {
   //   parameters.add("transactionrecordrequest", transactionRecordRequest.toString());
    //}
    //return OceanXML.request(url, parameters, httpTimeout).bodyToList(new TransactionRecord.TransactionRecordTransform());
  //}
}
